package models

import (
	"time"

	"github.com/astaxie/beego/orm"
)

type adminUser struct {
	Id       int
	Username string
	Password string
	Created  time.Time
	Update   time.Time
}

func (u *adminUser) TableName() string {
	return "cms_admin_user"
}

func init() {
	orm.RegisterModel(&adminUser{})
}

func GetAdminUserById(id int) *adminUser {
	if id == 0 {
		return nil
	}

	o := orm.NewOrm()
	user := adminUser{}
	user.Id = id
	err := o.Read(&user)

	if err == orm.ErrNoRows {
		return nil
	} else if err == orm.ErrMissPK {
		return nil
	}

	return &user
}

func CheckAdminUser(username string, password string) *adminUser {
	user := adminUser{}
	user.Username = username
	user.Password = password
	//以用户名字、用户密码查询该用户是否存在
	o := orm.NewOrm()
	o.Raw("SELECT * FROM cms_admin_user WHERE username = ? AND password = ?", user.Username, user.Password).QueryRow(&user)
	//查询用户存在返回:对象
	return &user
}
